<?php 
/**
 * Cette classe gère les films produits par Disney
 * 
 * @author Filament
 * @version 0.1
 * 
 * 10/06/11 : création de la page
 */

class Film {
	private $id;
	private $titre;
	private $sortieInternationale;
	private $sortieFrance;
	private $description;
	private $moyenne;
	private $statut;
	private $id_post_synopsis;
	private $ficheDetaillee;
	private $poster;
	private $typeFilm;
	private $production;
	
	public function __construct() {
		$num=func_num_args();
		
		switch($num) {
			case 1 :
				$idFilm = func_get_arg(0);
				$this->newFilm($idFilm);
				break;
			case 11 :
				$titreFilm = func_get_arg(0);
				$sortieInternationale = func_get_arg(1);
				$sortieFrance = func_get_arg(2);
				$description = func_get_arg(3);
				$moyenne = func_get_arg(4);
				$statut = func_get_arg(5);
				$synopsis = func_get_arg(6);
				$ficheDetaille = func_get_arg(7);
				$poster = func_get_arg(8);
				$typeFilm = func_get_arg(9);
				$production = func_get_arg(10);
				$this->ajouteFilm($titreFilm, $sortieInternationale, $sortieFrance, $description, $moyenne, $statut, $synopsis, $ficheDetaille, $poster, $typeFilm, $production);
				break;
		}
	}
	
	public function newFilm($id) {
		global $db;
		
		$sql = "SELECT
					f.titre
					, f.sortie_internationale
					, f.sortie_france
					, f.description
					, f.description_bitfield
					, f.description_uid
					, f.moyenne
					, f.statut
					, f.id_post_synopsis
					, f.fiche_detaillee
					, f.fiche_bitfield
					, f.fiche_uid
					, f.poster
					, f.type_film
					, tf.nom as nomTypeFilm
					, f.id_production
					, pr.nom as nomProduction
				FROM
					".FILM_TABLE." f
					, ".TYPE_FILM_TABLE." tf
					, ".PRODUCTION_FILM_TABLE." pr
					, ".USERS_TABLE." u
				WHERE
					f.id_film = '".$id."'
				LIMIT 0,1";
		$result = $db->sql_query($sql);
		$data = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		
		$this->id = $id;
		$this->titre = $data['titre'];
		$this->sortieInternationale = $data['sortie_internationale'];
		$this->sortieFrance = $data['sortie_france'];
		$this->moyenne = $data['moyenne'];
		$this->statut = $data['statut'];
		$this->id_post_synopsis = $data['id_post_synopsis'];
		$this->poster = $data['poster'];
		$this->typeFilm = array($data['type_film'], $data['nomTypeFilm']);
		$this->production = array($data['id_production'], $data['nomProduction']);
		
		//On encode la description
		$bbcode = new bbcode(base64_encode($data['description_bitfield']));
		$bbcode->bbcode_second_pass($data['description'], $data['description_uid'], $data['description_bitfield']);
		$this->description = bbcode_nl2br($data['description']);
		$this->description = smiley_text($this->description);
	}
	
	public function getId() {
		return $this->id;
	}
	
	public function getMoyenne() {
		return $this->moyenne;
	}
	
	public function getStatut() {
		return $this->statut;
	}
	
	public function getIdPostSynopsis() {
		return $this->id_post_synopsis;
	}
		
	public function getFicheDetaillee () {
		return $this->ficheDetaillee;
	}
	
	public function getTypeFilm($id = 1) {
		return $this->typeFilm[$id];
	}
	
	public function getProduction($id = 1) {
		return $this->production[$id];
	}
	
	public function getTitre() {
		return $this->titre;
	}
	
	public function getSortieInternationale() {
		return $this->sortieInternationale;
	}
	
	public function getSortieFrance() {
		return $this->sortieFrance;
	}
	
	public function getDescription() {
		return $this->description;
	}
	
	public function getPoster() {
		return $this->poster;
	}
	
	public function getPersonnalites() {
		global $db;
		
		$requete = $this->getRequetePersonnalites();
		$result = $db->sql_query($sqlPersonnalites, 5);
		while($personnalite = $db->sql_fetchrow($result)) {
			$personnalites[] = array(
				'id_personnalite'		=> $personnalite['id_personnalite'],
				'id_metier'				=> $personnalite['id_metier'],
				'nom_metier'			=> $personnalite['nom_metier'],
				'personnalite_nom'		=> $personnalite['personnalite_nom'],
				'personnalite_prenom'	=> $personnalite['personnalite_prenom']
			);
		}
		$db->sql_freeresult($result);
		
		return $personnalites;
	}
	
	public function getCommentaires() {
		global $user, $db;
		
		$sql = "SELECT 
					fc.id_commentaire
					, p.post_id
					, u.user_id
					, u.username
					, p.post_text
					, p.bbcode_bitfield
					, p.bbcode_uid
					, p.topic_id
					, p.post_time
				FROM
					" . FILM_COMMENTAIRE_TABLE . " fc
					, " . COMMENTAIRE_TABLE . " c
					, " . POSTS_TABLE . " p
					, " . USERS_TABLE  . " u
				WHERE
						fc.id_film = '".$this->id."'
					AND
						fc.id_commentaire = c.id_commentaire
					AND
						c.id_post = p.post_id
					AND
						p.poster_id = u.user_id";
		$result = $db->sql_query($sql);
		$return = "";
		while($com = $db->sql_fetchrow($result)) {
			//On encode le message
			$bbcode = new bbcode(base64_encode($com['bbcode_bitfield']));
			$bbcode->bbcode_second_pass($com['post_text'], $com['bbcode_uid'], $com['bbcode_bitfield']);
			$message = bbcode_nl2br($com['post_text']);
			$message = smiley_text($message);
			
			//Et ce qu'on enverra pour affichage :
			$return .= '<div class="commentaire">
							<p class="infos">Par <a href="../phpBB3//memberlist.php?mode=viewprofile&u=' . $com['user_id'] . '">'.$com['username'].'</a>, le ' . $user->format_date($com['post_time']).'.</p>
							<p class="com"> '. $message . '</p>
						</div>'; 
		}
		$db->sql_freeresult($result);
		
		return $return;
	}
	
	public function getMedias($limit = 0) {
		global $user, $db;
		
		$sql = "SELECT 
					m.id_media
					, m.nom
					, m.bbcode
					, m.bbcode_uid
					, m.bbcode_bitfield
					, m.description
					, m.description_bitfield
					, m.description_uid
					, u.username
					, u.user_id
					, m.timestamp
				FROM
					" . MEDIAS_TABLE . " m
					, " . FILM_MEDIAS_TABLE . " fm
					, " . USERS_TABLE . " u 
				WHERE
						fm.id_film = '" . $this->id . "'
					AND
						m.id_media = fm.id_media
					AND
						m.uploader = u.user_id
				ORDER BY m.timestamp DESC";
		
		//On exécute la requête :
		if($limit == 0)
			$result = $db->sql_query($sql);
		else
			$result = $db->sql_query_limit($sql, $limit);
			
		$return = "";
		if($result->num_rows > 0) {
			while($media = $db->sql_fetchrow($result)) {
				//On encode la description du media
				$bbcode = new bbcode(base64_encode($media['description_bitfield']));
				$bbcode->bbcode_second_pass($media['description'], $media['description_uid'], $media['description_bitfield']);
				$description_media = bbcode_nl2br($media['description']);
				$description_media = smiley_text($description_media);
				
				//On encode le bbcode du media
				$bbcode = new bbcode(base64_encode($media['bbcode_bitfield']));
				$bbcode->bbcode_second_pass($media['bbcode'], $media['bbcode_uid'], $media['bbcode_bitfield']);
				$mediaVue = bbcode_nl2br($media['bbcode']);
				$mediaVue = smiley_text($mediaVue);
				
				//L'affichage du média :
				$return .= '<div class="media">
								<p class="nom_media">'.$media['nom'].'</p>
								<p class="vue_media"> '. $mediaVue . '</p>
								<p class="infos">Par <a href="../phpBB3/memberlist.php?mode=viewprofile&u=' . $media['user_id'] . '">'.$media['username'].'</a>, le ' . $user->format_date($media['timestamp']).'.</p>
								<p class="desc_media">'.$description_media.'</p>
							</div>'; 
			}
		} else {
			$return = "Aucun m&eacute;dia n'a &eacute;t&eacute ajout&eacute;. Ajoutez en !";
		}
		$db->sql_freeresult($result);
		
		return $return;
	}
	
	public function getCasting() {
		global $user, $db;
		
		$sql = $this->getRequetePersonnalites();
		$result = $db->sql_query($sql);
		$return = "<ul>";
		while($perso = $db->sql_fetchrow($result)) {
			$return	.= '<li><a href="../personnalite/metier.php?id_metier='.$perso['id_metier'].'">' . $perso['nom_metier'] . '</a> : <a href="../personnalite/personnalite.php?id_personnalite='.$perso['id_personnalite'].'">' . $perso['personnalite_nom'] . ' ' . $perso['personnalite_nom'] . '</a>';
		}
		$return .= "</ul>";
		
		echo $return;
	}
	
	private function getRequetePersonnalites() {
		return "SELECT 
					pf.id_personnalite
					, pf.id_metier
					, m.nom as nom_metier
					, p.nom as personnalite_nom
					, p.prenom as personnalite_prenom
				FROM
					".PERSONNALITE_TABLE." p
					, ".PERSONNALITE_FILM_TABLE." pf
					, ".METIER_TABLE." m
				WHERE
						pf.id_film = '".$this->id."'
					AND
						pf.id_personnalite = p.id_personnalite
					AND
						pf.id_metier = m.id_metier
				ORDER BY
					id_metier, m.nom, p.nom";
	}
}
?>